/**
 * ====================================================================
 *
 *  Copyright 1999-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * .
 *
 * [Additional notices, if required by prior licensing conditions]
 *
 * 
 * @author Christian
 * @since
 * @version 
 * @Date 16.04.2014
 */
package de.sisoft.gcjack.communication.asyncstask;

import android.util.Log;
import de.sisoft.gcjack.communication.ConnectionManager;
import de.sisoft.gcjack.exception.DefaultConnectionException;
import de.sisoft.gcjack.exception.NoResultFoundException;
import de.sisoft.gcjack.exception.UserAlreadyVotedException;
import de.sisoft.gcjack.model.AsyncResult;
import de.sisoft.gcjack.model.GeoCache;
import de.sisoft.gcjack.model.User;

/**
 *
 */
// params, progress, result
public class PutGeoCacheVotingTask extends DefaultAsyncTask<AsyncResult, Integer, AsyncResult> {

	// private AsyncResponse callingActivity;
	private ConnectionManager			cnManager	= new ConnectionManager();
	private GeoCache					geocache;
	private User						user;

	private NoResultFoundException		noResultException;
	private DefaultConnectionException	connectionException;
	private UserAlreadyVotedException	userAlreadyVotedException;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.os.AsyncTask#onPreExecute()
	 */
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		this.callingActivity.getActivity().setProgressBarIndeterminateVisibility(true);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.os.AsyncTask#doInBackground(java.lang.Object[])
	 */
	protected AsyncResult doInBackground(AsyncResult... params) {

		for (AsyncResult businessObject : params) {
			if (businessObject instanceof GeoCache) {
				geocache = (GeoCache) businessObject;
			}
			if (businessObject instanceof User) {
				user = (User) businessObject;
			}
		}

		try {
			geocache = cnManager.voteGeoCache(geocache, user);

		} catch (NoResultFoundException nrfe) {
			Log.e("ERROR: ", nrfe.getMessage());
			this.noResultException = nrfe;
		} catch (DefaultConnectionException dce) {
			Log.e("ERROR: ", dce.getMessage());
			this.connectionException = dce;
		} catch (UserAlreadyVotedException uave) {
			Log.e("ERROR: ", uave.getMessage());
			this.userAlreadyVotedException = uave;
		}

		return geocache;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.os.AsyncTask#onProgressUpdate(Progress[])
	 */
	@Override
	protected void onProgressUpdate(Integer... progress) {
		super.onProgressUpdate(progress);
		// do some progress update stuff here
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
	 */
	@Override
	protected void onPostExecute(AsyncResult result) {

		this.callingActivity.getActivity().setProgressBarIndeterminateVisibility(false);

		if (this.noResultException == null && this.connectionException == null && userAlreadyVotedException == null) {
			callingActivity.processGeoCacheVotings(result);
		}

		if (this.noResultException != null) {
			callingActivity.handleException(noResultException);
			noResultException = null;
		}
		if (this.userAlreadyVotedException != null) {
			callingActivity.handleException(userAlreadyVotedException);
			userAlreadyVotedException = null;
		}

		// pDialog.dismiss();
		super.onPostExecute(result);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.os.AsyncTask#onCancelled(java.lang.Object)
	 */
	@Override
	protected void onCancelled(AsyncResult result) {
		if (cnManager.getHttpGet() != null) {
			cnManager.getHttpGet().abort();
		}
		if (cnManager.getHttpPost() != null) {
			cnManager.getHttpPost().abort();
		}
		this.callingActivity.getActivity().setProgressBarIndeterminateVisibility(false);
		super.onCancelled();
	}
}
